package com.apache.common.util;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TransTimeUtil {

    private static final String formatStr = "HH:mm:ss";

    private static SimpleDateFormat sdf = new SimpleDateFormat(formatStr);

    private Logger log = Logger.getLogger(getClass());

    /**
     * 判断到期时间是否在： start - end 之间
     *
     * @param start HH:mm:ss
     * @param end HH:mm:ss
     * @return
     */
    public static boolean checkTransTime(String start, String end) {
        try {
            if (isInZone(getLong(start), getLong(end), getCurrentTime())) {
                return true;
            }
        } catch (ParseException e) {
        }
        return false;
    }

    private static boolean isInZone(long tStart, long tEnd, long t) throws ParseException {
        return tStart <= t && t <= tEnd;
    }

    private static long getLong(String timeStr) throws ParseException {
        return sdf.parse(timeStr).getTime();
    }

    private static long getCurrentTime() throws ParseException {
        return getLong(sdf.format(new Date()));
    }
}
